package org.fengling.readingplatform.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.fengling.readingplatform.common.log.annotation.Log;
import org.fengling.readingplatform.common.log.enums.OperatorType;
import org.fengling.readingplatform.manager.service.SysRoleService;
import org.fengling.readingplatform.model.dto.system.SysRoleDto;
import org.fengling.readingplatform.model.entity.system.SysRole;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "角色管理接口")
@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    // 查询所有角色的方法
    @GetMapping("/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable("userId") long userId){
        Map<String, Object> map = sysRoleService.findAll(userId);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }

    //角色删除的方法
    @DeleteMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable("roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //角色修改的方法
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //角色添加的方法
    @Log(title = "角色管理：添加", businessType = 1,operatorType = OperatorType.MANAGE)
    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //角色列表的方法
    // current: 当前页  limit：每页显示记录数
    // SysRoleDto: 条件角色名称对象
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") Integer current,
                             @PathVariable("limit") Integer limit,
                             SysRoleDto sysRoleDto){
        //pageHelper插件实现分页
        PageInfo<SysRole> pageInfo =  sysRoleService.findByPage(sysRoleDto, current, limit);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

}